package com.ruoyi.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.utils.SimpleResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.AdvMembers;
import com.ruoyi.service.IAdvMembersService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * Controller
 *
 * @author ruoyi
 * @date 2024-05-13
 */
@RestController
@RequestMapping("/system/members")
public class AdvMembersController extends BaseController
{
    @Autowired
    private IAdvMembersService advMembersService;

    /**
     * 查询列表
     */
    @PreAuthorize("@ss.hasPermi('system:members:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdvMembers advMembers)
    {
        logger.debug("查询了用户列表");
        System.out.println(advMembers);
        startPage();
        //List<AdvMembers> list = advMembersService.selectAdvMembersList(advMembers);
        List<AdvMembers> list = advMembersService.selectAdvMembersListForp(advMembers);
        return getDataTable(list);
    }

    /**
     * 查询主播
     * @param
     * @return
     */

    @GetMapping("/listAnchor")
    public AjaxResult listAnchor()
    {
        //List<AdvMembers> list = advMembersService.selectAdvMembersList(advMembers);
        List<AdvMembers> list = advMembersService.selectAnchor();
        return AjaxResult.success(list);
    }


    @GetMapping("/list-two")
    public TableDataInfo listTwo(AdvMembers advMembers)
    {
        startPage();
        //List<AdvMembers> list = advMembersService.selectAdvMembersListSecond(advMembers);
        List<AdvMembers> list = advMembersService.selectAdvMembersListSecondForp(advMembers);
        return getDataTable(list);
    }

    /**
     * 导出列表
     */

    @PreAuthorize("@ss.hasPermi('system:members:export')")
    @Log(title = "会员管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AdvMembers advMembers)
    {

        List<AdvMembers> list = advMembersService.selectAdvMembersList(advMembers);
        ExcelUtil<AdvMembers> util = new ExcelUtil<AdvMembers>(AdvMembers.class);
        util.exportExcel(response, list, "数据");
    }

    /**
     * 获取详细信息
     */

    @PreAuthorize("@ss.hasPermi('system:members:query')")
    @GetMapping(value = "/{advMembersId}")
    public AjaxResult getInfo(@PathVariable("advMembersId") Long advMembersId)
    {
        return success(advMembersService.selectAdvMembersByAdvMembersId(advMembersId));
    }

    /**
     * 新增
     */
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('system:members:add')")
    @Log(title = "新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AdvMembers advMembers)
    {
        return toAjax(advMembersService.insertAdvMembers(advMembers));
    }

    /**
     * 修改
     */

    @PreAuthorize("@ss.hasPermi('system:members:edit')")
    @Log(title = "", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AdvMembers advMembers)
    {
        return toAjax(advMembersService.updateAdvMembers(advMembers));
    }

    /**
     * 删除
     */

    @PreAuthorize("@ss.hasPermi('system:members:remove')")
    @Log(title = "", businessType = BusinessType.DELETE)
	@DeleteMapping("/{advMembersIds}")
    public SimpleResult remove(@PathVariable Long[] advMembersIds)
    {
        advMembersService.deleteAdvMembersByAdvMembersIds(advMembersIds);
        return SimpleResult.success("删除成功");
    }

    /**
     * 小程序登录
     */

    @PostMapping("app_login")
    public SimpleResult appLogin(@RequestBody AdvMembers advMembers) {
        AdvMembers result = advMembersService.appLogin(advMembers);
        if(result.getWeOpenid().equals("")){
            return SimpleResult.success(result, "登录异常");
        }
        if(result == null) {
            return SimpleResult.success(result, "当前账户已被拉入黑名单");
        }
        return SimpleResult.success(result, "登录成功");
    }


    @PostMapping("resetrate")
    public SimpleResult resetRate(@RequestBody AdvMembers advMembers) {
        int rows = advMembersService.resetRate(advMembers);
        return SimpleResult.success("修改成功");
    }

    // 系统初始化

    @PostMapping("init_all")
    public SimpleResult initAll() {
        boolean res = advMembersService.initAll();
        return SimpleResult.success(res?"初始化成功":"初始化失败");
    }

    // 个人初始化

    @PostMapping("init_one")
    public SimpleResult initOne(@RequestBody AdvMembers advMembers) {
        System.out.println("初始化参数: =====> " + advMembers);
        boolean res = advMembersService.initOne(advMembers);
        return SimpleResult.success(res?"初始化成功":"初始化失败");
    }
}
